﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{
    public class IBufferReberNew : BufferBase
    {
        System.Data.DataTable dt;
        public List<Tuple<string, RebarCollection, double, int>> bufferRebarTuples = new List<Tuple<string, RebarCollection, double, int>>();
       
        public IBufferReberNew(string typeName, string pierNumber,
            int height, int width,System.Data.DataTable _dt) : base(typeName, pierNumber, height, width)
        {
            dt = _dt;
        }

       public override void SetRebarList()
        {
            DataView dv0 = dt.DefaultView;
            dt = dv0.ToTable(true, new string[] { "PierNo", "Station", "H", "L", "Angle" });

            System.Data.DataTable dtP = new System.Data.DataTable();
            DataView dv = dt.DefaultView;
            dtP = dv.ToTable(true, new string[] { "PierNo" });
            int N3 = 4;
            int N2 = 4;
            #region 全局变量
            for (int n = 0; n < dtP.Rows.Count; n++)
            {
                RebarList = new RebarCollection();
                double volumn = 0;
                string pierNo = dtP.Rows[n][0].ToString();
                DataRow[] drs= dt.Select("PierNo='" + pierNo + "'");
                for (int i = 0; i < drs.Length; i++)
                {
                    //string pierNo = drs[i]["PierNo"].ToString();
                    string station = drs[i]["Station"].ToString();
                    double H = double.Parse(drs[i]["H"].ToString()) * 1000;
                    double L = double.Parse(drs[i]["L"].ToString()) * 1000;
                    //double Span = double.Parse(drs[i]["SPAN"].ToString()) * 1000;
                    double angle= double.Parse(drs[i]["Angle"].ToString());
                   
                   
                    int aa = 0, bb = 0, cc = 0;
                    if (station.Contains("A"))
                    {
                        aa = 500 - 50 * 2 - 12 * 2;
                        bb = (int)L - 50 * 2 - 12 * 2;
                        N3 = (int)Math.Round(H / 150.0, MidpointRounding.AwayFromZero);
                        N3 = (int)H / 150;
                        RebarList.Add(new MORebarRecord(1, 0, station, eDB.DB12, N3, eShapeType.MO, aa, bb));
                        aa = (int)(400 + H - 50 );
                        cc = aa;
                        bb = (int)L - 50 * 2 - 12 * 2;
                        RebarList.Add(new MCRebarRecord(2, 0, station, eDB.DB20, aa, bb, cc, 4, eShapeType.MCB));
                        aa = (int)(400 + H - 50);
                        cc = aa;
                        bb = 500 - 50 * 2-2*12;
                        if (angle > 0)
                            bb = 383;
                        N2 = (int)Math.Round(L / 150.0, MidpointRounding.AwayFromZero);
                        N2 = (int)L / 150;
                        RebarList.Add(new MCRebarRecord(3, 0, station, eDB.DB12, aa, bb, cc, N2, eShapeType.MCB));
                      
                        aa = (int)(600 + H - 50 - 12);
                        cc = aa;
                        bb = 268;
                        RebarList.Add(new MCRebarRecord(4, 0, station, eDB.DB20, aa, bb, cc, 4, eShapeType.MCB));
                    }
                    else
                    {
                        aa = 500 - 50 * 2 - 12 * 2;
                        bb = (int)L - 50 * 2 - 12 * 2;
                        N3 = (int)Math.Round(H / 150.0, MidpointRounding.AwayFromZero);
                        N3 = (int)H / 150;
                        RebarList.Add(new MORebarRecord(1, 0, station, eDB.DB12, N3, eShapeType.MO, aa, bb));
                        aa = (int)(400 + H - 50);
                        cc = aa;
                        bb = (int)L - 50 * 2 - 12 * 2;
                        RebarList.Add(new MCRebarRecord(2, 0, station, eDB.DB20, aa, bb, cc, 4, eShapeType.MCB));
                        if (H<=1.2*1000)
                        {                          
                            aa = (int)(400 + H - 50);
                            cc = aa;
                            bb = 500 - 50 * 2 - 2 * 12;
                            if (angle > 0)
                                bb = 383;
                            N2 = (int)Math.Round(L / 150.0, MidpointRounding.AwayFromZero);
                            N2 = (int)L / 150;
                            RebarList.Add(new MCRebarRecord(3, 0, station, eDB.DB12, aa, bb, cc, N2, eShapeType.MCB));
                       
                            aa = (int)(600 + H - 50);
                            cc = aa;
                            bb = 268;
                            RebarList.Add(new MCRebarRecord(4, 0, station, eDB.DB20, aa, bb, cc, 4, eShapeType.MCB));
                        }
                        else
                        {
                            aa = (int)(400 + H - 50);
                            cc = aa;
                            bb = 500 - 50 * 2 - 2 * 12;
                            if (angle > 0)
                                bb = 383;
                            N2 = (int)Math.Round(L / 150.0, MidpointRounding.AwayFromZero);
                            N2 = (int)L / 150;
                            RebarList.Add(new MCRebarRecord(3, 0, station, eDB.DB16, aa, bb, cc, N2, eShapeType.MCB));                           
                            aa = (int)(600 + H - 50);
                            cc = aa;
                            bb = 268;
                            RebarList.Add(new MCRebarRecord(4, 0, station, eDB.DB28, aa, bb, cc, 4, eShapeType.MCB));
                        }
                    }
                    volumn += 0.5 * double.Parse(drs[i]["L"].ToString()) * double.Parse(drs[i]["H"].ToString());
                }
                Tuple<string, RebarCollection,double, int> tup = new Tuple<string, RebarCollection, double, int>(pierNo, RebarList, volumn,drs.Length);
                bufferRebarTuples.Add(tup);
            }
            #endregion
        }
    }
}
